<?php

require_once 'models/DAL/BannerDAO.php';

class Application_Model_DbTable_Banner extends Zend_Db_Table_Abstract {

    protected $_name = 'Banner';
    protected $_primary = 'idBanner';

    public function getAllBanners() {
        $rows = $this->fetchAll()->toArray();
        $return = array();

        foreach ($rows as $row) {
            $o = new Application_Model_DAO_Banner();

            $user = new Application_Model_DbTable_Usuario();

            $o->setCodUsuario((int) $row['codUsuario']);
            $o->setUsuario($user->getUserById($o->getCodUsuario()));
            $o->setIdBanner($row['idBanner']);
            $o->setResolucao($row['resolucao']);
            $o->setLocalizacao($row['localizacao']);
            $o->setTitulo($row['titulo']);
            $o->setImagePath($row['imagePath']);
            $o->setStatus(($row['status']) ? true : false);

            $return[] = $o;
        }
        return $return;
    }

    public function getAllActiveBanners() {

        $select = $this
                ->select()
                ->where('status = ?', 1);


        $rows = $this->fetchAll($select)->toArray();
        $return = array();

        foreach ($rows as $row) {
            $o = new Application_Model_DAO_Banner();

            $user = new Application_Model_DbTable_Usuario();

            $o->setCodUsuario((int) $row['codUsuario']);
            $o->setUsuario($user->getUserById($o->getCodUsuario()));
            $o->setIdBanner($row['idBanner']);
            $o->setResolucao($row['resolucao']);
            $o->setLocalizacao($row['localizacao']);
            $o->setTitulo($row['titulo']);
            $o->setImagePath($row['imagePath']);
            $o->setStatus(($row['status']) ? true : false);

            $return[] = $o;
        }
        return $return;
    }

    public function getBannerById(Application_Model_DAO_Banner $o) {

        $select = $this
                ->select()
                ->from($this->_name)
                ->where($this->_primary[1] . '=?', (int) $o->getIdBanner());

        $row = $this->fetchRow($select)->toArray();

        $o = new Application_Model_DAO_Banner();

        $user = new Application_Model_DbTable_Usuario();

        $o->setCodUsuario((int) $row['codUsuario']);
        $o->setUsuario($user->getUserById($o->getCodUsuario()));
        $o->setIdBanner($row['idBanner']);
        $o->setResolucao($row['resolucao']);
        $o->setLocalizacao($row['localizacao']);
        $o->setTitulo($row['titulo']);
        $o->setImagePath($row['imagePath']);
        $o->setStatus(($row['status']) ? true : false);


        return $o;
    }

    public function deleteBannerById(Application_Model_DAO_Banner $dao) {

         $return = $this->delete('idBanner =' . (int) $dao->getIdBanner());
        if ($return) {

        $file = str_replace(BASE_IMG . '/banners/', APPLICATION_PATH . '/../public/imgs/banners/', $dao->getImagePath());
        echo $file;

            try {
                unlink($file);
            } catch (Exception $e) {
                exit($e->getLine());
            }
        }
        return $return;
    }

}

